

#import "OCPageCollectionViewController.h"

@interface OCPageCollectionViewController () <UICollectionViewDelegate, UICollectionViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@end

@implementation OCPageCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    self.pageIndex = 1;
    
    kWEAKSELF
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [weakSelf.collectionView.mj_footer resetNoMoreData];
        [weakSelf requestHandlerWishIsRefresh:YES];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestHandlerWishIsRefresh:NO];
    }];
    [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
    self.collectionView.mj_footer = footer;
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
//        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(10));
//        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(10));
//        make.top.bottom.equalTo(self.view);
    }];
    
    [self.collectionView.mj_header beginRefreshing];
}

#pragma mark ------- Public Methods -------

- (void)requestHandlerWishIsRefresh:(BOOL)isRefresh { }

/**
 接口调用失败时停止刷新

 @param msg 失败信息
 */
- (void)apiFailed:(NSString *)msg {
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    [OCProgress_HUD showErrorWithStatus:msg];
}

/**
 使用该方法接受返回数据

 @param data 返回数据
 @param isRefresh 是否是刷新
 */
- (void)handleResponseData:(NSArray *)data isRefresh:(BOOL)isRefresh {
    if (isRefresh) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionData removeAllObjects];
        self.collectionData = [NSMutableArray arrayWithArray:data];
        if (self.collectionData.count == 0) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.pageIndex += 1;
        }
        [self.collectionView reloadData];
        
    } else {
        [self.collectionView.mj_footer endRefreshing];
        if (data.count == 0) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.pageIndex += 1;
            [self.collectionData addObjectsFromArray:data];
        }
        [self.collectionView reloadData];
    }
}

#pragma mark ------- DZNEmptyDataSetDelegate -------

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView;
{
    return [[NSAttributedString alloc] initWithString:@"暂时没有数据哟" attributes:nil];
}

//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView;
//{
//    return [UIImage imageNamed:@"no_data"];
//}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView;
{
    return YES;
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView; {
    scrollView.mj_footer.hidden = YES;
}

- (void)emptyDataSetDidDisappear:(UIScrollView *)scrollView; {
    scrollView.mj_footer.hidden = NO;
}

#pragma mark ------- UICollectionViewDelegate -------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionData.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[UICollectionViewCell kj_runtimeClassName] forIndexPath:indexPath];
    return cell;
}

#pragma mark ------- Lazy Methods -------

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreen_Width - 15 * 2 - 10) / 2, kFragmemtFloat(245));
        layout.minimumLineSpacing = 10;// 最小行间距
        layout.minimumInteritemSpacing = 0;// 最小列间距
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[UICollectionViewCell kj_runtimeClassName]];
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _collectionView.emptyDataSetSource = self;
        _collectionView.emptyDataSetDelegate = self;
    }
    return _collectionView;
}


@end
